# Copyright 2023 Google LLC
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# Description:
#   Build configurations for target platforms.

load("@bazel_skylib//lib:selects.bzl", "selects")

package(
    default_visibility = ["//:__subpackages__"],
)

############################# Build configurations #############################

config_setting(
    name = "linux",
    constraint_values = [
        "@platforms//os:linux",
    ],
)

config_setting(
    name = "linux_arm64",
    values = {"cpu": "aarch64"},
)

config_setting(
    name = "linux_k8",
    values = {"cpu": "k8"},
)

config_setting(
    name = "linux_arm",
    values = {"cpu": "arm"},
)

config_setting(
    name = "linux_armeabi",
    values = {"cpu": "armeabi"},
)

config_setting(
    name = "linux_armhf",
    values = {"cpu": "armhf"},
)

config_setting(
    name = "linux_armv7a",
    values = {"cpu": "armv7a"},
)

config_setting(
    name = "linux_riscv64",
    values = {"cpu": "riscv64"},
)

config_setting(
    name = "android",
    values = {"crosstool_top": "//external:android/crosstool"},
)

config_setting(
    name = "android_armv7",
    values = {
        "crosstool_top": "//external:android/crosstool",
        "cpu": "armeabi-v7a",
    },
)

config_setting(
    name = "android_arm64",
    values = {
        "crosstool_top": "//external:android/crosstool",
        "cpu": "arm64-v8a",
    },
)

config_setting(
    name = "android_x86",
    values = {
        "crosstool_top": "//external:android/crosstool",
        "cpu": "x86",
    },
)

config_setting(
    name = "android_x86_64",
    values = {
        "crosstool_top": "//external:android/crosstool",
        "cpu": "x86_64",
    },
)

config_setting(
    name = "windows_x86_64",
    values = {"cpu": "x64_windows"},
)

config_setting(
    name = "windows_x86_64_clang",
    values = {
        "compiler": "clang-cl",
        "cpu": "x64_windows",
    },
)

config_setting(
    name = "windows_x86_64_mingw",
    values = {
        "compiler": "mingw-gcc",
        "cpu": "x64_windows",
    },
)

config_setting(
    name = "windows_x86_64_msys",
    values = {
        "compiler": "msys-gcc",
        "cpu": "x64_windows",
    },
)

config_setting(
    name = "macos_x86_64",
    values = {
        "apple_platform_type": "macos",
        "cpu": "darwin_x86_64",
    },
)

config_setting(
    name = "macos_x86_64_legacy",
    values = {
        "apple_platform_type": "macos",
        "cpu": "darwin",
    },
)

config_setting(
    name = "macos_arm64",
    values = {
        "apple_platform_type": "macos",
        "cpu": "darwin_arm64",
    },
)

config_setting(
    name = "emscripten",
    values = {"crosstool_top": "@emsdk//emscripten_toolchain:everything"},
)

config_setting(
    name = "emscripten_wasm",
    values = {
        "crosstool_top": "@emsdk//emscripten_toolchain:everything",
        "cpu": "wasm",
    },
)

config_setting(
    name = "emscripten_wasmsimd",
    values = {
        "crosstool_top": "@emsdk//emscripten_toolchain:everything",
        "cpu": "wasm",
        "features": "wasm_simd",
    },
)

config_setting(
    name = "emscripten_wasmrelaxedsimd",
    values = {
        "crosstool_top": "@emsdk//emscripten_toolchain:everything",
        "cpu": "wasm",
        "features": "wasm_relaxed_simd",
    },
)

config_setting(
    name = "ios_arm64",
    values = {
        "apple_platform_type": "ios",
        "cpu": "ios_arm64",
    },
)

config_setting(
    name = "ios_arm64e",
    values = {
        "apple_platform_type": "ios",
        "cpu": "ios_arm64e",
    },
)

config_setting(
    name = "ios_sim_arm64",
    values = {
        "apple_platform_type": "ios",
        "cpu": "ios_sim_arm64",
    },
)

config_setting(
    name = "ios_x86_64",
    values = {
        "apple_platform_type": "ios",
        "cpu": "ios_x86_64",
    },
)

config_setting(
    name = "ios",
    values = {
        "apple_platform_type": "ios",
    },
)

config_setting(
    name = "watchos_arm64_32",
    values = {
        "apple_platform_type": "watchos",
        "cpu": "watchos_arm64_32",
    },
)

config_setting(
    name = "watchos_x86_64",
    values = {
        "apple_platform_type": "watchos",
        "cpu": "watchos_x86_64",
    },
)

config_setting(
    name = "tvos_arm64",
    values = {
        "apple_platform_type": "tvos",
        "cpu": "tvos_arm64",
    },
)

config_setting(
    name = "tvos_x86_64",
    values = {
        "apple_platform_type": "tvos",
        "cpu": "tvos_x86_64",
    },
)

# Duummy lexan config to avoid bazel errors.
config_setting(
    name = "windows_lexan",
    values = {"cpu": "x64_windows"},
)

# Dummy hexagon config to avoid bazel errors
config_setting(
    name = "hexagon",
    values = {
        "cpu": "hexagon",  # Expected to not-match anything
    },
)

######################## Composite build configurations #######################

selects.config_setting_group(
    name = "aarch32",
    match_any = [
        ":android_armv7",
        ":linux_arm",
        ":linux_armeabi",
        ":linux_armhf",
        ":linux_armv7a",
    ],
)

selects.config_setting_group(
    name = "aarch64",
    match_any = [
        ":android_arm64",
        ":ios_arm64",
        ":ios_arm64e",
        ":ios_sim_arm64",
        ":linux_arm64",
        ":macos_arm64",
        ":tvos_arm64",
        ":watchos_arm64_32",
    ],
)

selects.config_setting_group(
    name = "arm",
    match_any = [
        ":aarch32",
        ":aarch64",
    ],
)

selects.config_setting_group(
    name = "x86",
    match_any = [
        ":android_x86",
        ":android_x86_64",
        ":ios_x86_64",
        ":linux_k8",
        ":macos_x86_64",
        ":macos_x86_64_legacy",
        ":tvos_x86_64",
        ":watchos_x86_64",
        ":windows_x86_64",
        ":windows_x86_64_clang",
        ":windows_x86_64_mingw",
        ":windows_x86_64_msys",
    ],
)

selects.config_setting_group(
    name = "riscv",
    match_any = [":linux_riscv64"],
)
